<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */

namespace App\Service\Setting;

use App\Models\BeanSetting;
use App\Models\CombustionRecord;
use App\Service\BaseService;
use Illuminate\Support\Facades\Cache;

class BeanSettingService extends BaseService
{
    /**
     * @param $key
     * @return \Illuminate\Support\Collection
     */
    public function getSetting($key = null)
    {
        $seeting = Cache::remember('bean_seeting', now()->addHour(), function () {
            return BeanSetting::query()->pluck('value', 'key');
        });
        if ($key) {
            return $seeting[$key]??null;
        }
        return $seeting;
    }

    /**
     * 增加发行量.
     * @param $number
     */
    public function increaseCirculation($number = 1)
    {
        $number = round($number, 2);
        BeanSetting::query()->where('key', 'CIRCULATION')->increment('value', $number);
    }

    /**
     * 增加燃烧量.
     * @param mixed $number
     * @param null|mixed $description
     */
    public function increaseTheAmountOfCombustion($number = 1, $description = '团购燃烧')
    {
        $number = round($number, 2);
        BeanSetting::query()->where('key', 'BURNING_QUANTITY')->increment('value', $number);
        CombustionRecord::query()->create([
            'quantity' => $number,
            'description' => $description,
        ]);
    }
}
